import 'package:flutter/material.dart';
import 'package:udi_sys/common/dimens.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:io';
import 'package:udi_sys/http/http_connect_api.dart';
import 'package:dio/dio.dart';
/**
 * 登录页
 */

class InitLogin extends StatefulWidget {
  String title;

  InitLogin(this.title);

  @override
  _InitLoginState createState() => _InitLoginState();
}

class _InitLoginState extends State<InitLogin> {
  String _logErrorPhone = '';
  String _logErrorPwd = '';

//  用户名
  String _logPhone = '';

//  密码
  String _logPwd = '';

//  记住密码 默认为未选中
  bool _logIsRemember = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: Container(
        child: Column(
          children: [
            Container(
              child: Image(
                image: AssetImage('images/icon_login_log.png'),
                width: double.infinity,
                height: 200,
                fit: BoxFit.fitWidth,
              ),
            ),
            Container(
              margin: EdgeInsets.only(
                  left: MyDimens.size_layoutPaddingLeft,
                  top: MyDimens.size_login_marginTop,
                  right: MyDimens.size_layoutPaddingRight),
              child: Column(
                children: [
                  Container(
                    alignment: Alignment.centerLeft,
                    child: Text('请输入手机号',
                        style: TextStyle(color: Colors.black, fontSize: 18)),
                  ),
                  Theme(
                      data: ThemeData(primaryColor: Colors.black),
                      child: TextField(
                        // 只有当回车后才能监听到事件
                        onSubmitted: (String phone) {},
                        // 随输入内容变化而响应
                        onChanged: (String phone) {
                          setState(() {
                            // 当前有输入内容
                            if (!phone.isEmpty) {
                              _logPhone = phone;
                              _logErrorPhone = '';
                            } else {
                              // 当前没有输入内容
                              _logPhone = '';
                              _logErrorPhone = '';
                            }
                          });
                        },
                        cursorColor: Colors.black,
                        autofocus: true,
                        decoration: InputDecoration(
                          errorText: _logErrorPhone,
                          errorStyle: TextStyle(fontSize: 15),
                          errorBorder: UnderlineInputBorder(
                              borderSide: BorderSide(color: Colors.black)),
                          focusedErrorBorder: UnderlineInputBorder(
                              borderSide: BorderSide(color: Colors.black)),
                        ),
                      )),
                  Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '请输入有效密码',
                      style: TextStyle(color: Colors.black, fontSize: 18),
                    ),
                  ),
                  Theme(
                    data: ThemeData(primaryColor: Colors.black),
                    child: TextField(
                      // 只有当回车后才能监听到事件
                      onSubmitted: (String pwd) {},
                      // 随输入内容变化而响应
                      onChanged: (pwd) {
                        setState(() {
                          if (!pwd.isEmpty) {
                            _logPwd = pwd;
                            _logErrorPwd = '';
                          } else {
                            _logPwd = '';
                            _logErrorPwd = '';
                          }
                        });
                      },
                      cursorColor: Colors.black,
                      decoration: InputDecoration(
                          errorText: _logErrorPwd,
                          errorStyle: TextStyle(fontSize: 15),
                          errorBorder: UnderlineInputBorder(
                              borderSide: BorderSide(color: Colors.black)),
                          focusedErrorBorder: UnderlineInputBorder(
                              borderSide: BorderSide(color: Colors.black))),
                    ),
                  ),
                  Row(
                    children: [
                      Checkbox(
                        value: _logIsRemember,
                        onChanged: (value) {
                          setState(() {
                            _logIsRemember = !_logIsRemember;
                          });
                        },
                      ),
                      Text('记住密码')
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10),
                    height: 45,
                    child: RaisedButton(
                      onPressed: _checkLoginContentIsEmptyAndMsg,
                      child: Text(
                        '登   录',
                        style: TextStyle(color: Colors.white, fontSize: 19),
                      ),
                      color: Colors.blue,
                    ),
                    width: double.infinity,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    ));
  }

  /**
   * 检查内容是否为空并给相应的提示
   */
  _checkLoginContentIsEmptyAndMsg() {
    setState(() {
      if (_logPhone.isEmpty) {
        _logErrorPhone = '请输入手机号';
        return;
      }
      if (_logErrorPwd.isEmpty) {
        _logErrorPwd = '请输入有效密码';
        return;
      }
      _checkIsRememberPwd();
    });
  }

  /**
   * 判断是否设置记住密码
   */
  _checkIsRememberPwd() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if (_logIsRemember) {
      prefs.setString("logPhone", _logPhone);
      prefs.setString("logPwd", _logPwd);
    } else {
      prefs.setString("logPwd", '');
    }
    _userLogin();
  }

  /**
   * 登录
   */
  _userLogin() async{
    // var httpClient=HttpClient();
    // var uri=Uri.http(HTTPAPI.BASEHTTP+HTTPAPI.BASEHTTP_PATH, HTTPAPI.login);
    // print()
    Response response;
    var dio = Dio();
    response = await dio.post(HTTPAPI.BASEHTTP+HTTPAPI.BASEHTTP_PATH+HTTPAPI.login, data: {'username': 12, 'name': 'wendu'});
    print(response);
  }
}
